﻿using System;
using PS.WssSync.Utils;

namespace PS.WssSync
{
    class WssSync
    {
        private static AppProperties properties;
        private static WssSyncConfig syncConfig;

        static void Main(string[] args)
        {           
            try
            {
                properties = new AppProperties(args);                                
                syncConfig = WssSyncConfig.GetConfig(properties, false);
                
                EnsureSiteUrl();                                
                properties.Validate();
                
                if(Operations.Checkout == properties.Operation && null == syncConfig)
                {
                    syncConfig = WssSyncConfig.GetConfig(properties, true);
                }

                FileSync fileSync = new FileSync(properties, syncConfig);
                fileSync.Synchronize();
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine("error: " + ex.Message);
                ConsoleHelper.WriteInvalidArguments();
            }
            catch (Exception ex)
            {
                Console.WriteLine("generic error: " + ex.Message);
            }
        }

        private static void EnsureSiteUrl()
        {
            if(!string.IsNullOrEmpty(properties.DirUrl) || null == syncConfig)
            {
                return;
            }

            if(!string.IsNullOrEmpty(syncConfig.SiteLocation))
            {
                properties.DirUrl = syncConfig.SiteLocation;
            }
        }
    }
}
